from PIL import Image


def imageprepare(file_name):
    # file_name='d:/ddd.png'#导入自己的图片地址
    # in terminal 'mogrify -format png *.jpg' convert jpg to png
    im = Image.open(file_name).convert('L')
    im.show()
    im = im.resize((28, 28))
    # print(im)

    im.save("sample.png")
    # plt.imshow(im)
    # plt.show()
    tv = list(im.getdata())  # get pixel values
    # print(type(tv))

    # normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
    # tva = [((255 - x) * 1.0) / 255.0 for x in tv]
    # print(tva)
    # print(type(tva))
    return tv


if __name__ == '__main__':
    result = imageprepare('image/mnist_train_0.png')
